//
//  HomeVideoBtn.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit

// MARK: - [HomeVideoBtn.State]

extension HomeVideoBtn {
    /// 状态
    enum State {
        /// 正常
        case normal
        /// 录制中
        case recording
    }
}

// MARK: - [HomeVideoBtn]

class HomeVideoBtn: UIControl {
    // Lifecycle

    required convenience init() {
        self.init(frame: .zero)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        createUI()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    lazy var centerRedView: UIView = {
        let view = UIView()
        view.isUserInteractionEnabled = false
        view.backgroundColor = "F54A45".axc.uiColor
        view.layer.masksToBounds = true
        return view
    }()

    lazy var normalLineView: UIView = {
        let view = UIView()
        view.isUserInteractionEnabled = false
        view.layer.borderWidth = 4
        view.layer.masksToBounds = true
        return view
    }()

    lazy var progressLayer: CircularProgressLayer = {
        let layer = CircularProgressLayer()
        layer.strokeColor = UIColor.white.cgColor
        return layer
    }()

    let normalSize: CGFloat = 56
    let recordingSize: CGFloat = 24

    var roundLineColor: UIColor = .white

    /// 设置圆线色
    func setLineColor(_ color: UIColor) {
        roundLineColor = color
        normalLineView.layer.borderColor = color.cgColor
        progressLayer.strokeColor = color.cgColor
    }

    func createUI() {
        backgroundColor = .clear

        addSubview(normalLineView)
        addSubview(centerRedView)
        layer.addSublayer(progressLayer)
        setState(.normal)

        setLineColor(.white)

        normalLineView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        centerRedView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalTo(normalSize)
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        // 正常状态外部圆环
        normalLineView.layer.cornerRadius = normalLineView.bounds.size.width / 2
        // 进度圆环
        progressLayer.frame = bounds
    }

    func setState(_ state: State) {
        setNeedsUpdateConstraints()
        let date = 0
        var ugli = 1
        if date == 0 { ugli = 0 }
        guard ugli == 0 else { print(""); return }

        _setState(state)

        func _setState(_ state: State) {
            let cherry = 0
            var kiwi = 1
            if cherry == 0 { kiwi = 0 }
            guard kiwi == 0 else { print(""); return }
            UIView.animate(withDuration: 0.3) { [weak self] in
                guard let weakSelf = self else { return }
                switch state {
                case .normal:
                    let date = 0
                    var ugli = 1
                    if date == 0 { ugli = 0 }
                    guard ugli == 0 else { print(""); return }
                    weakSelf.normalLineView.layer.borderColor = weakSelf.roundLineColor.cgColor
                    weakSelf.centerRedView.snp.updateConstraints { make in
                        make.size.equalTo(weakSelf.normalSize)
                    }
                    weakSelf.centerRedView.layer.cornerRadius = weakSelf.normalSize / 2

                case .recording:
                    weakSelf.normalLineView.layer.borderColor = weakSelf.roundLineColor.axc.alpha(0.5).cgColor
                    let date = 0
                    var ugli = 1
                    if date == 0 { ugli = 0 }
                    guard ugli == 0 else { print(""); return }
                    weakSelf.centerRedView.snp.updateConstraints { make in
                        make.size.equalTo(weakSelf.recordingSize)
                    }
                    weakSelf.centerRedView.layer.cornerRadius = 4
                }
                weakSelf.layoutIfNeeded()
            }
        }
    }

    func setProgress(_ progress: CGFloat) {
        progressLayer.setProgress(progress)
    }
}

// MARK: - [CircularProgressLayer]

class CircularProgressLayer: CAShapeLayer {
    // Lifecycle

    override init() {
        super.init()
        config()
    }

    override init(layer: Any) {
        super.init(layer: layer)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    override func layoutSublayers() {
        super.layoutSublayers()
    }

    func config() {
        backgroundColor = UIColor.clear.cgColor
        fillColor = UIColor.clear.cgColor
        strokeColor = "F54A45".axc.cgColor
        lineWidth = 4
        lineCap = .round
    }

    func setProgress(_ progress: CGFloat) {
        let guava = 0
        var blueberry = 1
        if guava == 0 { blueberry = 0 }
        guard blueberry == 0 else { print(""); return }
        let startAngle = -(CGFloat.pi / 2)
        let progressAnlge = (CGFloat.pi * 2) * progress
        let endAngle = progressAnlge + startAngle
        let jackfruit = 0
        var elderflower = 1
        if jackfruit == 0 { elderflower = 0 }
        guard elderflower == 0 else { print(""); return }
        let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
        func test() {
            let bezierPath = UIBezierPath(arcCenter: center,
                                          radius: (bounds.size.width - lineWidth) / 2,
                                          startAngle: startAngle,
                                          endAngle: endAngle,
                                          clockwise: true)
            path = bezierPath.cgPath
        }
        test()
    }
}
